package org.medicalinstrument.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.medicalinstrument.bean.RentOrderDetail;
import org.medicalinstrument.bean.RentOrderVo;
import org.medicalinstrument.bean.SearchOption;
import org.medicalinstrument.dao.RentOrderDetailDao;
import org.medicalinstrument.database.DataSourceUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class RentOrderDetailDaoImpl implements RentOrderDetailDao {

    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public boolean add(Connection conn, RentOrderDetail orderDetail) throws SQLException {
        String sql = "insert into rentorderdetail values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        int update = qr.update(conn,sql, orderDetail.getCustPhone(), orderDetail.getAccountNo(),
                orderDetail.getOrderTime(), orderDetail.getProductQR(), orderDetail.getRentTime(),
                orderDetail.getReturnTime(), orderDetail.getRentOrderId(), orderDetail.getRentDays(),
                orderDetail.getRentMoney(),orderDetail.getRealRentTime(),orderDetail.getRealRentDays(),
                orderDetail.getRent(),orderDetail.getRentMoneyTime(),orderDetail.getOrderState());
        return update > 0;

    }

    @Override
    public List<RentOrderVo> getByAccountNo(String accountNo) throws SQLException {
        String sql = "select distinct a.*,b.productName,b.productColor," +
                "b.productPhoto,b.rentPrice,b.introduction from rentorderdetail " +
                "as a join product as b where a.productQR=b.productQR and accountNo = ?";
        List<RentOrderVo> rentOrderDetails = qr.query(sql, new BeanListHandler<RentOrderVo>(RentOrderVo.class), accountNo);
        return rentOrderDetails;
    }

    @Override
    public Boolean updateRentMoneyTime(RentOrderDetail detail) throws SQLException {
        String sql = "update rentorderdetail set rentMoneyTime=? where accountNo=? and productQR=? and rentOrderId=?";
        int update = qr.update(sql, detail.getRentMoneyTime(), detail.getAccountNo(), detail.getProductQR(), detail.getRentOrderId());
        return update > 0;
    }

    @Override
    public Boolean updateRuturnMessage(Connection conn, RentOrderDetail detail) throws SQLException {
        String sql = "update rentorderdetail set returnTime=?, realRentDays=?, rent=? where accountNo=? and productQR=? and rentOrderId=?";
        int update = qr.update(conn,sql, detail.getReturnTime(), detail.getRealRentDays(), detail.getRent(),
                detail.getAccountNo(), detail.getProductQR(), detail.getRentOrderId());
        return update > 0;

    }

    @Override
    public Boolean updateOrderState(Connection conn, RentOrderDetail detail) throws SQLException {
        String sql = "update rentorderdetail set orderState=? where accountNo=? and productQR=? and rentOrderId=?";
        int update = qr.update(conn,sql, detail.getOrderState(), detail.getAccountNo(), detail.getProductQR(),
                detail.getRentOrderId());
        return update > 0;
    }

    @Override
    public List<RentOrderVo> getByRentOrderId(String rentOrderId) throws SQLException {
        String sql = "select distinct a.*,b.productName,b.productColor," +
                "b.productPhoto,b.rentPrice,b.introduction from rentorderdetail" +
                " as a join product as b where a.productQR=b.productQR and rentOrderId =?";
        List<RentOrderVo> query = qr.query(sql, new BeanListHandler<RentOrderVo>(RentOrderVo.class), rentOrderId);
        //System.out.println(query);
        return query;
    }

    @Override
    public List<RentOrderVo> getByOption(SearchOption searchOption) throws SQLException {
        String sql = "select distinct a.*,b.productName,b.productColor," +
                "b.productPhoto,b.rentPrice,b.introduction from rentorderdetail" +
                " as a join product as b where a.productQR=b.productQR and ";

        if (!("".equals(searchOption.getRentOrderId()))) {
            sql += "rentOrderId like '%" + searchOption.getRentOrderId() + "%' and ";
        }
        if (!("".equals(searchOption.getAccountNo()))) {
            sql += "accountNo like '%" + searchOption.getAccountNo() + "%' and ";
        }
        if (!("".equals(searchOption.getCustPhone()))) {
            sql += "custPhone like '%" + searchOption.getCustPhone() + "%' and ";
        }
        if (!("".equals(searchOption.getOrderState()))) {
            sql += "orderState=" + searchOption.getOrderState() + " and ";
        }
        /*if (searchOption.getRentTimeLeft() != null && searchOption.getRentTimeRight() != null) {
            sql += "rentTime between " + searchOption.getRentTimeLeft() + " and " + searchOption.getRentTimeRight() + " and ";
        }*/


        sql += "1=1";
        System.out.println(sql);
        List<RentOrderVo> query = qr.query(sql, new BeanListHandler<RentOrderVo>(RentOrderVo.class));
        //System.out.println(query);
        return query;
    }

    @Override
    public Boolean updateOrderStates(RentOrderDetail detail) throws SQLException {
        String sql = "update rentorderdetail set orderState=? where accountNo=? and productQR=? and rentOrderId=?";
        int update = qr.update(sql, detail.getOrderState(), detail.getAccountNo(), detail.getProductQR(),
                detail.getRentOrderId());
        return update > 0;
    }

}
